package com.hfkh.keyun.controller.frontend;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Course;
import com.hfkh.keyun.entity.Major;
import com.hfkh.keyun.entity.Unit;
import com.hfkh.keyun.exception.BusinessException;
import com.hfkh.keyun.mapper.MajorMapper;
import com.hfkh.keyun.mapper.UnitMapper;
import com.hfkh.keyun.service.CourseService;
import com.hfkh.keyun.vo.CourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/api/course")
@Api(tags = "课程管理")
public class CourseControllerApp {
    @Resource
    private CourseService courseService;
    @Resource
    private UnitMapper mapper;
    @Resource
    private MajorMapper majorMapper;

    @GetMapping("/selAllCourse")
    public List<Course> selCourse(){
        List<Course> courses = courseService.list(null);
        courses.forEach(System.out::println);
        return courses;
    }
    @ApiOperation("按照课程名称查询课程")
    @GetMapping("/queryCourse")
    public void getName(@RequestParam("courseName") String courseName){
        QueryWrapper<Course> wrapper= new QueryWrapper<>();
        wrapper.eq("course_name",courseName);
        Course course = courseService.getOne(wrapper);
        Unit unit = mapper.selectById(course.getUnitId());
        course.setUnit(unit);
        Major major = majorMapper.selectById(course.getMajorId());
        course.setMajor(major);
        System.out.println("单元表对象"+unit);
        System.out.println("专业表对象"+major);
        System.out.println("课程名称："+course.getCourseName()+
                "课程详情："+course.getCourseDesc()+
                "课程价格："+course.getCoursePrice()+
                "课程所属单元名称："+course.getUnit().getUnitName()+
                "课程所属专业名称："+course.getMajor().getMajorName());
    }
    /**
     * 按照专业id查询课程以及单元名称
     */
    @ApiOperation("按专业id查询课程")
    @GetMapping("/queryPage2")
    public ApiResult queryPage(Integer page,
                               Integer limit,
                               Integer majorId){
        Page<Course> pageInfo = new Page<>(page, limit);
        Page<Course> coursePage = courseService.selectCourseAndUnitPage(pageInfo, majorId);
        return ApiResult.success(coursePage);
    }
}
